﻿using System;
using System.Collections.Generic;
using PhoneCore.Framework;
using PhoneCore.Framework.Bootstrap;
using PhoneCore.Framework.Configuration;
using PhoneCore.Framework.Diagnostic.Tracing;
using PhoneCore.Framework.IoC;
using PhoneCore.Framework.Views.Command;
using SecureBox.UI.Infrastructure;
using SecureBox.UI.Infrastructure.Plugins;

namespace SecureBox.UI.ViewModel
{
    public class SettingsViewPageModel : ViewModelBase
    {
        [Dependency]
        public IBootstrapperService BootstrapService { get; set; }

        [Dependency]
        public ITrace Trace { get; set; }

        public SettingsViewPageModel()
        {
            GoToHealth = new ExecuteCommand(DoGoToHealth);
            DefaultTraceController controller = new DefaultTraceController(TraceFactory.GetTrace() as DefaultTrace, "Resources/Templates/DefaultTraceReport.txt");
            TotalTraceRecordText = String.Format("log contains {0} records", controller
                .Statistic()
                .TotalRecords);

            RangeChangedCommand = new ExecuteCommand(OnRangeChanged);
            PasswordMinimum = 3;
            PasswordMaximum = 24;
            //OnRangeChanged();
        }

        public string TotalTraceRecordText { get; set; }
        public double PasswordMinimum { get; set; }
        public double PasswordMaximum { get; set; }
        public string PasswordLabel { get; set; }

        public ExecuteCommand RangeChangedCommand { get; set; }

        /// <summary>
        /// Sets new range of password generator setting
        /// </summary>
        public void OnRangeChanged()
        {
            int min = Convert.ToInt16(PasswordMinimum);
            int max = Convert.ToInt16(PasswordMaximum);
            PasswordLabel = String.Format("Password length {0}-{1}", min,max);
            //write settings
            SettingService.Save(Constants.PassGenMinimumSettingName, min);
            SettingService.Save(Constants.PassGenMaximumSettingName, max);

            //NOTE try to recall why I've written this (Ilya Builuk)
            //call update of PassGen task to reread settings
            BootstrapService.GetPlugin<PassGenPlugin>().Update();

            int level = SettingService.Load<int>(Constants.TraceLevelSetting);
            SelectedTraceLevel = ResolveTraceLevel(level);
            RaisePropertyChanged("PasswordLabel");
        }

        private string _selectedTraceLevel;
        public string SelectedTraceLevel
        {
            get { return _selectedTraceLevel; }
            set
            {
                int level = ResolveTraceLevel(value);
                SettingService.Save(Constants.TraceLevelSetting, level);
                Trace.Level = level;
                _selectedTraceLevel = value;
                RaisePropertyChanged("SelectedTraceLevel");
            }
        }

        private string ResolveTraceLevel(int level)
        {
            switch (level)
            {
                case 1:
                    return "Warn";
                case 2:
                    return "Error";
                case 3:
                    return "Fatal";
                default:
                    return "Info";
            }
        }

        private int ResolveTraceLevel(string text)
        {
            switch (text)
            {
                case "Warn":
                    return 1;
                case "Error":
                    return 2;
                case "Fatal":
                    return 3;
                default:
                    return 0;
            }
        }

        public ExecuteCommand GoToHealth
        {
            get;
            private set;
        }

        private void DoGoToHealth()
        {

           NavigationService.NavigateTo("Health", new Dictionary<string, object>()
           { { "Health.Trace", TraceFactory.Default } });

        }
    }
}
